package com.bjqh.edu.UI.Activity.Login;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.EasyActivity;
import com.bjqh.edu.Bean.Code;
import com.bjqh.edu.Bean.Login;
import com.bjqh.edu.MainActivity;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.StudentPersonal.MySettingActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.PerPasswordActivity;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bjqh.edu.Widget.CountDownTimerUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class PasswordSettingsActivity extends EasyActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.et_Phone)
    EditText etPhone;
    @BindView(R.id.et_identify_code)
    EditText etIdentifyCode;
    @BindView(R.id.get_iden_card)
    Button getIdenCard;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_confirm_passwprd)
    EditText etConfirmPasswprd;
    @BindView(R.id.tv_ok)
    Button tvOk;
    private int timeS = 90;
    private Handler handler = new Handler();
    private int numcode;
    private String rsaPassword;
    private Runnable runnable;
    private String userId;
    private boolean runningDownTimer;     //判断是否在倒计时
    private boolean flag;
    String card;
    private LoadDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_password_settings);
        ButterKnife.bind(this);
        initViews();
        initData();
        initEvents();
    }

    @Override
    protected void initViews() {
        tvTitle.setText("密码设置");
        dialog = new LoadDialog(this,"正在找回密码");



    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "报告小主，手机号还没有填呢~", Toast.LENGTH_SHORT).show();
                    etPhone.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etPassword.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "报告小主，密码还没有填呢~", Toast.LENGTH_SHORT).show();
                    etPassword.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etConfirmPasswprd.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "报告小主，确认密码还没有填呢~", Toast.LENGTH_SHORT).show();
                    etConfirmPasswprd.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(etIdentifyCode.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "报告小主，验证码还没有填呢~", Toast.LENGTH_SHORT).show();
                    etIdentifyCode.requestFocus();
                    return;
                }

                if (card != null && !card.equals(etIdentifyCode.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "您输入的验证码有误，请重新获取", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!etPassword.getText().toString().equals(etConfirmPasswprd.getText().toString())) {
                    Toast.makeText(getApplicationContext(), "报告小主，您输入的两次密码不一致哦~~", Toast.LENGTH_SHORT).show();

                    return;

                }
                dialog.initDialog(PasswordSettingsActivity.this,"正在找回密码");

                OkGo.post(AppConstants.BASE_URL + "/user/add.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        .params("passWord", AppConstants.md5(etPassword.getText().toString()))
                        .params("mobile", etPhone.getText().toString())
                        .params("type", "0")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Gson gson = new Gson();
                                Login login = gson.fromJson(s, Login.class);

                                if (login.getMsg().equals("success")) {
                                    LoadDialog.closeDialog();
                                    Toast.makeText(PasswordSettingsActivity.this, "小主，您的密码找回成功,您可重新登录哦~", Toast.LENGTH_SHORT).show();
                                    Log.e("密码找回", s);
                                    OkGo.post(AppConstants.BASE_URL + "/sys/exit.shtml")
                                            .tag(this)
                                            .cacheKey("cacheKey")
                                            .cacheMode(CacheMode.DEFAULT)
                                            .execute(new StringCallback() {
                                                @Override
                                                public void onSuccess(String s, Call call, Response response) {
                                                    Log.e("退出登录 ", s);

                                                }

                                                @Override
                                                public void onError(Call call, Response response, Exception e) {
                                                    super.onError(call, response, e);
                                                    Toast.makeText(PasswordSettingsActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                                }
                                            });

                                    Intent logoutIntent = new Intent(PasswordSettingsActivity.this, LoginActivity.class);
                                    logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(logoutIntent);
                                    LoadDialog.closeDialog();

                                } else {
                                    Toast.makeText(PasswordSettingsActivity.this, "报告小主，我在地球上没有找到您的手机号，您是不是输错了？", Toast.LENGTH_SHORT).show();
                                   LoadDialog.closeDialog();
                                }

                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                Toast.makeText(PasswordSettingsActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                                Log.e("密码找回", "找回失败");
                            }
                        });

            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getIdenCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    etPhone.setError("请检查是否填写手机号");
                    etPhone.requestFocus();
                    return;
                }

                OkGo.post(AppConstants.BASE_URL + "/sys/shorMessage.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        .params("mobile", etPhone.getText().toString())
                        .params("type", "0")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Gson gson = new Gson();
                                Code code = gson.fromJson(s, Code.class);
                                if (code.getMsg().equals("OK")) {
                                    final CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getIdenCard, 60000, 1000); //倒计时1分钟
                                    mCountDownTimerUtils.start();
                                    Toast.makeText(PasswordSettingsActivity.this, "报告小主，验证码请求成功", Toast.LENGTH_SHORT).show();
                                    numcode = (int) ((Math.random() * 9 + 1) * 100000);
                                    Log.e("验证码请求成功", s);

                                    card = code.getCode();
                                } else if (code.getMsg().equals("moblie is null")) {
                                    Toast.makeText(PasswordSettingsActivity.this, "报告小主，我在地球上没有找到您的手机号，您是不是输错了？", Toast.LENGTH_SHORT).show();
                                    Log.e("手机号不存在", s);

                                } else if (code.getMsg().equals("invalid mobile number")) {
                                    Toast.makeText(PasswordSettingsActivity.this, "报告小主，您的手机号是无效的", Toast.LENGTH_SHORT).show();
                                }


                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                Toast.makeText(PasswordSettingsActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

    }

}
